import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('pages/Login.vue')
const Register = () => import('pages/Register.vue')
const Profile = () => import('pages/Profile.vue')
const Attention = () => import('pages/Attention.vue')
const EditUser = () => import('pages/EditUser.vue')
const MyComments = () => import('pages/MyComments.vue')
const MyCollections = () => import('pages/MyCollections.vue')
const Home = () => import('pages/Home.vue')
const Search = () => import('pages/Search.vue')
const Article = () => import('pages/Article.vue')
const MoreComment = () => import('pages/MoreComment.vue')
const ColumnAdminister = () => import('pages/ColumnAdminister.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/home', component: Home },
  { path: '/login', name: 'Login', component: Login },
  { path: '/register', component: Register },
  { path: '/profile', component: Profile },
  { path: '/attention', component: Attention },
  { path: '/my-comments', component: MyComments },
  { path: '/my-collections', component: MyCollections },
  { path: '/edituser', component: EditUser },
  { path: '/search', component: Search },
  { path: '/article/:id', component: Article },
  { path: '/more-comment/:id', component: MoreComment },
  { path: '/columnAdminister', component: ColumnAdminister }
]

const router = new VueRouter({
  routes
})

const authRoute = ['/profile', '/attention', '/edituser', '/columnAdminister']

router.beforeEach((to, from, next) => {
  if (authRoute.includes(to.path)) {
    const token = localStorage.getItem('accessToken')
    if (token) return next()
    return next('/login')
  }
  next()
})

export default router
